<!DOCTYPE html>
<html lang="en">

	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<meta http-equiv="X-UA-Compatible" content="ie=edge">
		<title>华胜后台管理</title>
		<link rel="stylesheet" type="text/css" href="css/unified.css">
		<script src="plugins/layui/layui.js"></script>
	</head>

	<body>

		<div class="kit-doc">
			<!-- 标题 -->
			<div class="kit-doc-title">
				<fieldset>
					<legend>
						<a name="blockquote"> 订单</a><span class="titles">线下销售</span>
					</legend>
				</fieldset>
			</div>
			<!-- 标题 end-->

			<!-- 概述 -->
			<div class="boderbox">
				<div class="rowtop">
					<button class="layui-btn addbtn openwinbtn" data-method="addmang">添加+</button>

				</div>
				<div class="layui-tab-item layui-show">
					<table class="layui-table " lay-filter="table100" id="ordertable"></table>

				</div>
				<!--</div>-->

				<!--支付状态表格处理-->
				<script type="text/html" id="paymentStateTp">
					{{# if(d.paymentState == "PendingPay"){ }}
					<b style="color: #f30000;"> 未付款</b> {{# } else if(d.paymentState == "PartialPay"){ }}
					<b style="color: #ff9800;"> 部分付款</b> {{# } else if(d.paymentState == "Success"){ }}
					<b style="color: #009688;"> 已付款</b> {{# } }}

				</script>
				
				<!--订单状态表格处理-->
				<script type="text/html" id="orderStateTp">

					{{# if(d.orderState == "Waiting_for_delivery" || d.orderState == null ){ }}
					<b style="color: #f30000;"> 等待发货</b> 
					{{# } else if(d.orderState == "Already_shipped"){ }}
					<b style="color: #ff9800;"> 待收货</b> 
					{{# } else if(d.orderState == "Success"){ }}
					<b style="color: #0aab25;"> 已收货</b> 
					
					{{# } else if(d.orderState == "Complete"){ }}
					<b style="color: #009688;"> 订单完成</b>
					
					
					{{# } }}

				</script>
				
				<!--操作按钮菜单-->
				<script type="text/html" id="operationTp">
					{{# if(d.orderState == "Waiting_for_delivery" || d.orderState == null){ }}
					<a class="layui-btn " lay-event="selectThis" >查看订单</a>
					{{# } else if(d.orderState == "Already_shipped"){ }}
					<a class="layui-btn " lay-event="selectThis" >查看订单</a><a class="layui-btn layui-bg-blue" lay-event="YesThis" >确认收货</a>
					{{# } else if(d.orderState == "Complete"){ }}
						<a class="layui-btn " lay-event="selectThis" >查看订单</a>
					{{# } else if(d.orderState == "Success"){ }}
						<a class="layui-btn " lay-event="OverThis" >完成订单</a>
					{{# } }}
					
					<!--<a class="layui-btn " lay-event="selectThis" >查看</a><a class="layui-btn layui-bg-blue" lay-event="YesThis" >确认收货</a>-->

				</script>
				
				<script id="openbox" type="text/html">
					<table class="layui-table " lay-filter="table100" id="opensee"  lay-skin="row"  lay-even  >
						<thead><tr><td>状态</td><td>商品编号</td><td>商品条码</td><td>商品标题</td><td>购买数量</td><td>库存数量</td></tr></thead>
						<tbody></tbody>
					</table>
				</script>
				<script src="js/jquery-3.2.1.min.js"></script>
				<script type="text/javascript" src="js/allajax.js"></script>

				<script>
					//				
					//			 {{#  if(d.paymentState == "Unfinished"){ }}
					//				   		未付款
					//				  {{#  } else if(d.paymentState == "PartialPay"){  }}
					//				    	部分付款
					//				  {{#  }else if(d.paymentState == "Success"){  } }}
					//				  已付款
					//				   {{#  }else { }}
					//				   失败
					//				   {{#  } }}

					layui.use(['layer', 'form', 'element', 'table', 'upload'], function() {
						var layer = layui.layer,
							form = layui.form,
							element = layui.element,
							table = layui.table,
							upload = layui.upload;
						var curr = 1;
						var limit = 100;
						var thisindex = 1;
						var datafirst = "'sorting':'generate_time','tosort':'desc'"; //默认前缀data字符串
						var data7 = "{" + datafirst + ",'paymentMethod':'Reality'}";
						var tableArray = new Array();
						productList(data7, 7, curr, limit);

						function productList(data, type, curr, limit, callback) {
							AjaxPostFn('system/orderinfo/select?page=' + curr + '&pagesize=' + limit, data, function(jsonData) {
								 tableArray = new Array();
								if(jsonData.pageInfo.size != 0) {
									$.each(jsonData.pageInfo.list, function(i, objjson) {
										obj = objjson.orderInfoWithBLOB;
										tableArray.push({
											'operation': '',
											"orderNumber": obj.orderNumber, //出库
											"orderId": obj.orderId,
											"userId": obj.userId,
											"openid": obj.openid,
											"orderAddress": obj.orderAddress,
											"orderAdcode": obj.orderAdcode,
											"contactName": obj.contactName,
											"contactPhone": obj.contactPhone,
											"contactTelephone": obj.contactTelephone,
											"shippingMethod": obj.shippingMethod,
											"orderState": obj.orderState,
											"orderStateIp": obj.orderState,
											"paymentMethod": obj.paymentMethod,
											"paymentAmount1": (obj.paymentAmount / 100).toFixed(2),
											"paymentState": obj.paymentState,
											"paymentStateTp": obj.paymentState,
											"expressNumber": obj.expressNumber,
											"expressBusiness": obj.expressBusiness,
											"orderRemarks": obj.orderRemarks,
											"generateTime": getMyDate(obj.generateTime),
											"invoiceType": obj.invoiceType,
											"invoiceHeader": obj.invoiceHeader,
											"deliveryNumber": obj.deliveryNumber,
											"orderMachineIp": obj.orderMachineIp,
											"deviceInfo": obj.deviceInfo,
											"tradeType": obj.tradeType,
											"transactionId": obj.transactionId,
											"paySuccessTime": obj.paySuccessTime,
											"orderType": obj.orderType,
											"scoreUse": obj.scoreUse,
											"orderProcess": obj.orderProcess,
											"orderPickupCode": obj.orderPickupCode,
											"merchantNumber": obj.merchantNumber,
											"evaluationState": obj.evaluationState,
											"wechatPayResult": obj.wechatPayResult,
											"wechatRefundResult": obj.wechatRefundResult,
											"orderCommodityVos": objjson.orderCommodityVos,
											"bizCommodityForSales": objjson.bizCommodityForSales,
											"realIncome": (obj.realIncome / 100).toFixed(2),
											"newMoney":"0"

										});

									})
								}

								//展示已知数据
								var tableIns = table.render({
									elem: '#ordertable',
									cols: [
										[ //标题栏
											{
												type:'numbers',
												fixed: 'left',
												title: '行号'
											},
											{
												field: 'id',
												minWidth: '0%',
												width: '0%',
												type: 'space',
												style: 'display:none'
											}, {
												field: 'orderNumber',
												width: 180,
												title: '订单号',
												sort: true
											}, {
												field: 'contactName',
												width: 200,
												title: '收货人',
												sort: true
											},

											{
												field: 'contactPhone',
												width: 150,
												title: '收货人手机'
											}, {
												field: 'orderAddress',
												width: 400,
												title: '订单地址'
											},
											{
												field: 'orderRemarks',
												width: 100,
												title: '订单备注'
											}, {
												field: 'orderNumber',
												width: 180,
												title: '订单号'
											},
											{
												field: 'shippingMethod',
												width: 100,
												title: '装运方式'
											}, {
												field: 'expressNumber',
												width: 100,
												title: '快递单号'
											}, {
												field: 'expressBusiness',
												width: 100,
												title: '订单备注'
											}, {
												field: 'generateTime',
												width: 300,
												title: '生成时间'
											}, {
												field: 'operation',
												width: 200,
												title: '操作',
												templet: '#operationTp',
												fixed: 'right'
											},
											
											{
												field: 'orderStateTp',
												width: 100,
												title: '订单状态',
												templet: '#orderStateTp',
												fixed: 'right',
												sort: true
											},
											{
												field: 'paymentStateTp',
												width: 100,
												title: '付款状态',
												templet: '#paymentStateTp',
												fixed: 'right',
												sort: true
											},{
												field: 'newMoney',
												width: 100,
												title: '新增收款' + modDot,
												fixed: 'right',
												edit: 'text'
											},{
												field: 'realIncome',
												width: 100,
												title: '实收金额' ,
												fixed: 'right'
											
											},{
												field: 'paymentAmount1',
												width: 100,
												title: '应付金额',
												fixed: 'right'
											}

										]
									],
									data: tableArray,
									id: 'ordertable',
									size: 'lg',
									height: 'full-250',
									even: true,
									page: true, //是否显示分页
									done: function() {
										//图片点击放大
										TopimgFn();
									}
								});
							})

						}//list函数结束
						
						
						//监听单元格编辑
						table.on('edit(table100)', function(obj) {
							var value = obj.value //得到修改后的值
								,data = obj.data //得到所在行所有键值
								,field = obj.field; //得到字段
//							data.updateTime = getnowhour();
//							data.updateId = '1';
							var thisdata  = {};
							
							thisdata.orderNumber  = data.orderNumber ; 
							thisdata.realIncome =  data.newMoney *100 ;
							
							AjaxPostFn('system/orderinfo/modifyrealityorder', JSON.stringify(thisdata), function(jsonData) {
								if(jsonData.htmlState.status == 200) {
									layer.msg("修改成功");
									
								}else{
									layer.msg("修改失败,"+jsonData.htmlState.message);
									
								}
								productList(data7, 7, curr, limit);
							})
					
						});
						
						table.on('tool(table100)', function(obj) {
							var data = obj.data;
							
							if(obj.event === 'selectThis') {
								//查看订单下的产品库存情况
								layer.open({
									type:1
								  	,skin: 'layui-layer-molv'
						          	,title: '查看订单号:'+data.orderNumber+' 订单'
						          	,maxmin: 'true'
						          	,shade: 0
						          	,area: ['80%' , '80%']
						         	,content:$("#openbox").html()
						          	,skin: 'demo-class'
						          	,btn:["确认发货","关闭"]
						          	,yes:function(index,layero){
						          	  //按钮1的回调
						         
										console.log(JSON.stringify(data));
										if(data.orderState == 'Already_shipped'){
											layer.msg("该订单商品已经出库了！");
										}else{
											$.each(data.orderCommodityVos, function(voidx,voobj) {
								          	  var ForSales  = data.bizCommodityForSales[voidx];
								          	  var insertjson = '{  "orderNumber": "'+data.orderNumber+'", "barCode": "'+ForSales.barCode+'", "theNum": '+voobj.buyAmount+', "locationId": 1, "status": 0,"createId": 1, "createTime": "'+new Date().getTime()+'", "commodityNumber": "'+ForSales.commodityNumber+'", "merchantNumber": 1, "commodityId": "'+ForSales.commodityId+'"}   ';
								          	  
									          	AjaxPostFn('system/repository/prepare/insert', insertjson, function(data1 ) {
													if(data1.htmlState.status == 200){
														layer.msg("订单出货成功");
														$("#opensee tbody tr:eq("+voidx+") td:eq(0)").html("<b style='color: #009688;'>出库成功</b>");
														$("#opensee tbody tr:eq("+voidx+") td:eq(0)").attr("data-stats","1");
														
														
													}else{
														$("#opensee tbody tr:eq("+voidx+") td:eq(0)").html("<b style='color: #f30000;'>出库异常</b>");
													}
									          	},voidx);
	
								          	})
											
											setTimeout(function(){
												$.each($("#opensee tbody tr"),function(ttridx,trobj){
													console.log($("#opensee tbody tr:eq("+ttridx+") td:eq(0)").attr("data-stats") == 1)
													if($("#opensee tbody tr:eq("+ttridx+") td:eq(0)").attr("data-stats") == 1){
													
														AjaxPostFn('system/orderinfo/examine/newexamine', "{'orderNumber':'"+data.orderNumber+"','orderStatus':'Already_shipped'}", function(data) {
															if	(data.htmlState.status == 200){
																productList(data7, 7, curr, limit);
															}
														})
													}
													
												})
											},500);
										}
									
						          	},btn2:function(index,layero){
						          	  //按钮2的回调
						          	},cancel:function(){
						           	 //右上关闭的回调
						           	 productList(data7, 7, curr, limit);
						          	},success:function(){
						          		//渲染商品数据
						          		console.log(JSON.stringify(data)) 
										var vostr ;
										$.each(data.bizCommodityForSales, function(voidx,voobj) {
//												console.log(data.bizCommodityForSales.commodityId)
											if(data.orderState == 'Already_shipped'){
												vostr += "<tr><td  data-stats='1'><b style='color: #009688;'>已出库</b></td>" ;
											}else{
												vostr += "<tr><td  data-stats='0'>等待出库</td>" ;
												
											}
											vostr += "<td>"+voobj.commodityNumber+"</td>" +
													 "<td>"+voobj.barCode+"</td>" +
													 "<td>"+voobj.title+"</td>" +
													 "<td>"+data.orderCommodityVos[voidx].buyAmount+"</td>" +
													 "<td></td></tr>";
										});
										
//										
										$("#opensee tbody").html(vostr);
										
										
										
									}
						          	
										
								});
//							          		AjaxPostFn('system/orderinfo/select', {'orderNumber':data.orderNumber}, function(jsonData) {
////							          		console.log(jsonData);
//							          			
//							          			$(".openTable").html(jsonData)
//							          		})
						      
							}else if(obj.event === 'YesThis'){
								layer.confirm('温馨提示:请确认好客户已签收订单内的商品.', {
								  	btn: ['确认','取消'] //按钮
								}, function(){
								  	layer.msg('商品已确认签收', {icon: 1});
								  	AjaxPostFn('system/orderinfo/examine/newexamine', "{'orderNumber':'"+data.orderNumber+"','orderStatus':'Success'}", function(data) {
										if(data.htmlState.status == 200){
											productList(data7, 7, curr, limit);
										}
									})
								  
								}, function(){
								  
								});
								
								
								
							}else if(obj.event === 'OverThis'){
								layer.confirm('温馨提示:请确认好货物已签收且已收取全额款项.', {
								  	btn: ['确认','取消'] //按钮
								}, function(){
								  	layer.msg('订单确认完成', {icon: 1});
								  	AjaxPostFn('system/orderinfo/examine/newexamine', "{'orderNumber':'"+data.orderNumber+"','orderStatus':'Complete'}", function(data) {
										if	(data.htmlState.status == 200){
											productList(data7, 7, curr, limit);
										}
									})
								  
								}, function(){
								  
								});
								
								
								
							}
						})	

						$('.openwinbtn').on('click', function() {
		                    var othis = $(this),
		                        method = othis.data('method');
		                    active[method] ? active[method].call(this, othis) : '';
		                });
								
						var active = {
						    addmang:function(){
				
								var addindex = layer.open({
								  type: 2,
								  title:"选择库存内的商品",
								  area: ['100%', '100%'],
								  maxmin: true,
								  shade: 0,
								  content: 'order_in.html',
								  cancel: function(){ 
								  		productList(data7, 7, curr, limit);
								  }
								});
								
							}
						}
					});
				</script>
			</div>
			<!-- 概述 end-->
		</div>
	</body>

</html>